package com.police.activity;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.astrob.ESurfing.Util.StringUtil;
import com.astrob.mapapi.route.Route;
import com.astrob.mapapi.route.Step;
import com.police.R;

public class RouteAdapter extends BaseAdapter {
	private final String ROUTE_IMAGE_URL = "http://ditu.astrob.com/images/route/";

	private Context mContext = null;
	private Route mRoute;

	public RouteAdapter(Context context, Route route) {
		mContext = context;
		mRoute = route;
	}
	
	public void setRoute(Route route) {
		this.mRoute = route;
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return mRoute.getLegs().get(0).getSteps().size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return mRoute.getLegs().get(0).getSteps().get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		ViewHolder holder;
		if (null == convertView) {
			convertView = LinearLayout.inflate(mContext, R.layout.pathplanitem,
					null);
			holder = new ViewHolder();

			holder.dirView = (ImageView) convertView
					.findViewById(R.id.pathplan_turn_dir);
			holder.nameView = (TextView) convertView
					.findViewById(R.id.pathplan_turn_text);
			holder.disView = (TextView) convertView
					.findViewById(R.id.pathplan_turn_dis);

			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}

    	Step data = mRoute.getLegs().get(0).getSteps().get(position);
//		StringBuffer buffer = new StringBuffer();
//		buffer.append(data.getAction().toLowerCase());
//		buffer.append(".png");
//		holder.dirView.setImageUrl(ROUTE_IMAGE_URL + buffer.toString());
		holder.dirView.setImageResource(getDirDrawable(data.getAction()));

		StringBuffer buffer2 = new StringBuffer();
		buffer2.append(data.getDistance());
		buffer2.append("米");
		holder.disView.setText(buffer2.toString());
		holder.nameView.setText(StringUtil.trimToEmpty(data.getGuidance()));

		return convertView;
	}

	private int getDirDrawable(String action) {
		StringBuffer buffer = new StringBuffer();
		buffer.append("R.drawable.");
		buffer.append(action.toLowerCase());
		int drawable = 0;
		if (StringUtil.equals(action, "T_LEFT")) {
			drawable = R.drawable.t_left;
		} else if (StringUtil.equals(action, "T_RIGHT")) {
			drawable = R.drawable.t_right;
		} else if (StringUtil.equals(action, "T_SLIGHT_RIGHT")) {
			drawable = R.drawable.t_slight_right;
		} else if (StringUtil.equals(action, "T_SLIGHT_LEFT")) {
			drawable = R.drawable.t_slight_left;
		} else if (StringUtil.equals(action, "T_SHARP_LEFT")) {
			drawable = R.drawable.t_sharp_left;
		} else if (StringUtil.equals(action, "T_SHARP_RIGHT")) {
			drawable = R.drawable.t_sharp_right;
		} else if (StringUtil.equals(action, "T_STRAIGHT")) {
			drawable = R.drawable.t_straight;
		} else if (StringUtil.equals(action, "T_U_TURN")) {
			drawable = R.drawable.t_u_turn;
		} else if (StringUtil.equals(action, "T_FORK_LEFT")) {
			drawable = R.drawable.t_fork_left;
		} else if (StringUtil.equals(action, "T_FORK_MIDDLE")) {
			drawable = R.drawable.t_fork_middle;
		} else if (StringUtil.equals(action, "T_FORK_RIGHT")) {
			drawable = R.drawable.t_fork_right;
		} else if (StringUtil.equals(action, "T_ENTER_ROUNDABOUT")) {
			drawable = R.drawable.t_enter_roundabout;
		} else if (StringUtil.equals(action, "T_ENTER_ROUNDABOUT_EXIT_1")) {
			drawable = R.drawable.t_enter_roundabout;
		} else if (StringUtil.equals(action, "T_EXIT_ROUNDABOUT")) {
			drawable = R.drawable.t_exit_roundabout;
		} else if (StringUtil.equals(action, "T_ENTER_FERRY")) {
			drawable = R.drawable.t_enter_ferry;
		} else if (StringUtil.equals(action, "T_EXIT_FERRY")) {
			drawable = R.drawable.t_exit_ferry;
		} else if (StringUtil.equals(action, "T_ENTER_TUNNEL")) {
			drawable = R.drawable.t_enter_tunnel;
		} else if (StringUtil.equals(action, "T_EXIT_TUNNEL")) {
			drawable = R.drawable.t_exit_tunnel;
		} else if (StringUtil.equals(action, "T_ENTER_SERVICE")) {
			// 服务区没图片
			drawable = R.drawable.t_enter_sideroad;
		} else if (StringUtil.equals(action, "T_EXIT_SERVICE")) {
			drawable = R.drawable.t_enter_sideroad;
		} else if (StringUtil.equals(action, "T_TOLL")) {
			drawable = R.drawable.t_toll;
		} else if (StringUtil.equals(action, "T_ENTER_HIGHWAY")) {
			drawable = R.drawable.t_enter_highway;
		} else if (StringUtil.equals(action, "T_ENTER_RAMP")) {
			drawable = R.drawable.t_enter_ramp;
		} else if (StringUtil.equals(action, "T_EXIT_RAMP")) {
			drawable = R.drawable.t_exit_ramp;
		} else if (StringUtil.equals(action, "T_ENTER_BRIDGE")) {
			drawable = R.drawable.t_enter_bridge;
		} else if (StringUtil.equals(action, "T_EXIT_BRIDGE")) {
			drawable = R.drawable.t_exit_bridge;
		} else if (StringUtil.equals(action, "T_ENTER_MAINROAD")) {
			drawable = R.drawable.t_enter_mainroad;
		} else if (StringUtil.equals(action, "T_ENTER_SIDEROAD")) {
			drawable = R.drawable.t_enter_sideroad;
		} else if (StringUtil.equals(action, "T_ORIGIN")) {
			drawable = R.drawable.origin;
		} else if (StringUtil.equals(action, "T_DEST_ON_LEFT")
				|| StringUtil.equals(action, "T_DEST_ON_RIGHT")) {
			drawable = R.drawable.dest;
		}
		return drawable;
	}

	static class ViewHolder {
		ImageView dirView;
		TextView nameView;
		TextView disView;
	}
}
